/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import model.PhuCap;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class PhuCapDao {
    private static Session session;
    
    public PhuCapDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertPhuCap
     */
    public boolean insertPhuCap(String maPhuCap, String tenPhuCap, Integer giaTri, String ghiChu){
        try {
            session.beginTransaction();
            
            PhuCap phuCap = new PhuCap(maPhuCap, tenPhuCap, giaTri, ghiChu);
            
            session.save(phuCap);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * updatePhuCap
     */
    public boolean updatePhuCap(String maPhuCap, String tenPhuCap, Integer giaTri, String ghiChu){
        try {
            session.beginTransaction();
            
            PhuCap phuCap = (PhuCap) session.get(PhuCap.class, maPhuCap);
            phuCap.setGhiChu(ghiChu);
            phuCap.setGiaTri(giaTri);
            phuCap.setTenPhuCap(tenPhuCap);
            
            session.update(phuCap);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * deletePhuCap
     */
    public boolean deletePhuCap(String maPhuCap){
        try {
            session.beginTransaction();
            
            PhuCap phuCap = (PhuCap) session.get(PhuCap.class, maPhuCap);
            
            session.delete(phuCap);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
}
